package com.example.databindingdemo.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.example.databindingdemo.R;
import com.squareup.picasso.Picasso;

/**
 * @author: njb
 * @date: 2022/9/18 0:36
 * @desc: 自定义BindingAdapter
 */
public class BindingAdapterImage {
    @BindingAdapter("image")
    public static void setImage(ImageView image,int imgResource){
        image.setImageResource(imgResource);
    }

    @BindingAdapter("netWorkImage")
    public static void setNetWorkImage(ImageView imageView,String imgUrl){
       if(!TextUtils.isEmpty(imgUrl)){
           Picasso.get().load(imgUrl).placeholder(R.drawable.ic_launcher_background)
                   .error(R.mipmap.ic_launcher).centerCrop().resize(300,300)
                   .into(imageView);
       }else {
           imageView.setBackgroundColor(Color.RED);
       }
    }

    @BindingAdapter("padding")
    public static void setPadding(View view,int oldPadding, int newPadding){
        if(oldPadding != newPadding){
            view.setPadding(newPadding,newPadding,newPadding,newPadding);
        }
    }
}
